package com.zang.concurrent.collections.blockingqueue;

import com.zang.concurrent.collections.ThreadPool;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * BlockingQueue测试类
 *
 * product 写入Queue的线程
 * consumer 读取Queue的线程
 *
 * ArrayBlockingQueue 数组队列 有界限的 FIFO(先进先出)
 *
 * @author Zhang Qiang
 * @Date 2019/9/6 17:14
 */
public class BlockingQueueExample {

    public static void main(String[] args) throws InterruptedException {
//        blockingTest();
        te();
    }

    public static void te(){
        String subDiscuz = "<script type=\"text/javascript\" src=\"http://discuz.com:900/api/uc.php?time=1573196874&code=5aa9RniDdtJr41kSCF8GTgAwkTes10Hyq4PIDK%2FVlcVmmMRIm8ZEmxlw0KRBuQnAGV0PEP49LdcbH5IPBz1" +
                "%2F6IQ056ZAMKd4q1EwvWUy2lQU7vYmHxYTPYkUvv97DgAe9Tc5jnl92na0TPfsy0CL8EAwbREWTSu7aYkA3w\" reload=\"1\"></script>";
        subDiscuz.substring(subDiscuz.indexOf("src="));
        Matcher matcher = Pattern.compile("\"").matcher(subDiscuz);
        int x = 0;
        int x1 =0;
        int x2 =0;
        while (matcher.find()){
            x++;
            System.out.println(" x:" + x + ", m: " + matcher.end());
            if (x==3){ x1 = matcher.end(); }
            if (x==4) x2 = matcher.end();
            if (matcher.end() > subDiscuz.length())break;
        }

        String url = subDiscuz.substring(x1,x2);
        System.out.println("url:" + url);
        if (url.endsWith("\\") && url.endsWith("\"")){
            url = url.substring(0, url.length() -1);
        }
        System.out.println("url:" + url);

    }


    public static void blockingTest() throws InterruptedException {
        BlockingQueue queue = new ArrayBlockingQueue(1024);
        Producer producer = new Producer(queue);
        Consumer consumer = new Consumer(queue);

        //创建线程池
        ExecutorService cachedThreadPool = ThreadPool.getPool().get("cachedThreadPool");
        //执行线程
        //创建
        cachedThreadPool.execute(producer);
        //读取
        cachedThreadPool.execute(consumer);
        Thread.sleep(4000);
    }


    /**
     * run() start() test
     * 测试run与start执行顺序
     * @return
     */
    static void runTest(){

        Thread thread = new Thread(){
            @Override
            public void run() {
                pongRun();
            }
        };
        //直接调用run()打印
        thread.run();
        // 线程就绪，等待当前线程完毕以后获取到cpu时间片再执行run()方法
        //thread.start();
        System.out.println(" start ");

    }
    static void pongRun(){
        System.out.println(" run ");
    }


}
